mod particle;
mod shape;

use ggez::{Context, GameResult, GameError, graphics, glam::Vec2};
use rand::Rng;

pub struct State{
    shapes: Vec<shape::Shape>,
}

impl State{
    pub fn new() -> State {
        let mut shapes = vec![];
        for _i in 0..10 {
            let mut rng = rand::thread_rng();
            let origin = Vec2 { x: rng.gen_range(0.0..700.0), y: rng.gen_range(0.0..500.0) };
            let size = Vec2 { x: rng.gen_range(20.0..100.0), y: rng.gen_range(20.0..100.0) };
            let shape = shape::Shape::new(origin, size);
            shapes.push(shape);
        }
        State { shapes }
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, ctx: &mut Context) -> GameResult {

        self.shapes.retain_mut(|shape| {
            shape.update(ctx);
            !shape.is_dead()
        });
        
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);
        for shape in &mut self.shapes {shape.display(ctx, &mut canvas)};
        canvas.finish(ctx)?;
        Ok(())
    }
}